Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @Qix-
Taking advantage of the fact that we no longer need to support Python 2.7, I think we can largely simplify how we manage strings encoding.
This avoids the use of hacky
ProxyBufferStreamWrapper
class, the dubiousto_unicode()
andto_bytes()
functions, and the conditional statement inwrite_stream()
.Basically, we just format the exception as an unicode string, and we let the
sys.stderr
stream handles encoding, no need to deal with.buffer
bytes.You may notice one side-effect: utf-8 characters like
"天"
are no longer displayed as it on ascii terminals. I think this is actually the correct way to do it.One thing I don't understand with the current implementation is that in one hand we test encoding of
└
and fallback to->
on error, on the other hand we manage to print天
in all cases. This results in traceback which may look like-> "天"
. It's paradoxical, either we can display utf8 characters or we can't. I suspect that this is a source of errors for the problems encountered by some users.By writing not encoded unicode to
sys.stderr
, the unprintable characters are automatically escapted with thesurrogateescape
policy, and hence displays-> "\u5929"
on ascii terminals,└ "天"
otherwise.Also, I replaced
sys.getpreferredencoding()
withSTREAM.encoding
, because we are writing toSTREAM
(sys.stderr
) so why not use its specified encoding? Usingsys.getpreferredencoding()
proved to display mojibake characters to some users, so maybe this will fix it.I made some tests on both Linux and Windows and compared exception formatting between standard and
better_exceptions
based on locale and IO encoding. The handling of utf8 characters is now identitical to what is done by the default exception handler, so I think this should reduce problems due to encoding.This pull request is made for the
python3_only
branch.